package net.pulseproject.commons.gui;

import java.awt.event.ActionEvent;

import javax.swing.JFrame;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

public class SafeCatchingActionListenerImpl extends SafeActionListenerImpl implements SafeCatchingActionListener {

	private static final Log LOG = LogFactory.getLog(SafeCatchingActionListenerImpl.class);
	
	private final ExceptionDialogFactory dialogFactory;

	private final JFrame parent;

	private final String actionDescription;
	
	private final Action action;
	
	@Inject
	public SafeCatchingActionListenerImpl(
			final ExceptionDialogFactory dialogFactory,
			@Assisted final JFrame parent,
			@Assisted final String actionDescription,
			@Assisted final Action action) {
		
		this.dialogFactory = dialogFactory;
		this.parent = parent;
		this.actionDescription = actionDescription;
		this.action = action;
	}
	
	@Override public final void onActionThrewException(final ActionEvent event, final Exception exception) {
		LOG.error("GUI action threw exception!", exception);
		
		final ExceptionDialog dialog = this.dialogFactory.create(this.parent, this.actionDescription, exception);
		dialog.setVisible(true);
	}

	@Override public final void action(final ActionEvent event) {
		this.action.doAction(event);
	}
	
}
